package engine;

import views.GamePlayView;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Picture;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Paint.Align;
import android.graphics.drawable.PictureDrawable;
import android.os.Vibrator;
import android.util.Log;
import android.view.View;

import com.tetris.project.R;
import com.tetris.project.Shr_Res;

import framework.Physics;


public class BoardUI extends View{
	
	Bitmap[] bgImageLand;
	Bitmap[] bgImagePort;
	Bitmap menuButton, menuButtonPressed, pauseButton, pauseButtonPressed, playButton, playButtonPressed, gameImageBackground, toastBackground;
	Paint myTextPaint, myImagePaint, myNextPiecePaint, myScorebgPaint , myToastPaint, myToastBGPaint;
	public FakeButton pause;
	public FakeButton menu;
	RectF gameRectBackground;
	String level, dificulty;
	String toast;// = "Awesome!!";
	Picture bla;
	PictureDrawable nextPiece;
	Rect rectNextPiece, rectNextPiecebg, background, rectScorebg, rectToast;
	//int picture = 0;  //index number for the background images array
	int w;   //Window width
	int h;   //Window height
	int pw;   //Piece width
	int ph;   //Piece height
	int toastState = 0;
	int toastDelay = 8;
	public static int counter = 0;
	Context mContext;
	Vibrator v;
	
	public BoardUI(Context context){
		super(context);
		
		toast = getResources().getString(R.string.block_toast_message);
		
		mContext = context;
		
		playButton = BitmapFactory.decodeResource(context.getResources(), R.drawable.play_button);
		playButtonPressed = BitmapFactory.decodeResource(context.getResources(), R.drawable.play_button_pressed);
		
		pauseButton = BitmapFactory.decodeResource(context.getResources(), R.drawable.pause_button);
		pauseButtonPressed = BitmapFactory.decodeResource(context.getResources(), R.drawable.pause_button_pressed);

		menuButton = BitmapFactory.decodeResource(context.getResources(), R.drawable.menu_button);
		menuButtonPressed = BitmapFactory.decodeResource(context.getResources(), R.drawable.menu_button_pressed);
		
		gameImageBackground = BitmapFactory.decodeResource(context.getResources(), R.drawable.board_background);
		
		toastBackground = BitmapFactory.decodeResource(context.getResources(), R.drawable.toast_bg);
		
		bgImageLand = new Bitmap[6];
		bgImagePort = new Bitmap[6];
		bgImageLand[0] = BitmapFactory.decodeResource(context.getResources(), R.drawable.bg0_land);
		bgImagePort[0] = BitmapFactory.decodeResource(context.getResources(), R.drawable.bg0_port);
		bgImageLand[1] = BitmapFactory.decodeResource(context.getResources(), R.drawable.bg1_land);
		bgImagePort[1] = BitmapFactory.decodeResource(context.getResources(), R.drawable.bg1_port);
		bgImageLand[2] = BitmapFactory.decodeResource(context.getResources(), R.drawable.bg2_land);
		bgImagePort[2] = BitmapFactory.decodeResource(context.getResources(), R.drawable.bg2_port);
		bgImageLand[3] = BitmapFactory.decodeResource(context.getResources(), R.drawable.bg3_land);
		bgImagePort[3] = BitmapFactory.decodeResource(context.getResources(), R.drawable.bg3_port);
		bgImageLand[4] = BitmapFactory.decodeResource(context.getResources(), R.drawable.bg4_land);
		bgImagePort[4] = BitmapFactory.decodeResource(context.getResources(), R.drawable.bg4_port);
		bgImageLand[5] = BitmapFactory.decodeResource(context.getResources(), R.drawable.bg5_land);
		bgImagePort[5] = BitmapFactory.decodeResource(context.getResources(), R.drawable.bg5_port);
		
		menu = new FakeButton(0, 0, menuButton.getHeight(), menuButton.getWidth());
		pause = new FakeButton(0, 0, pauseButton.getHeight(), pauseButton.getWidth());
		
		gameRectBackground = new RectF();
		
		dificulty = new String(getDificulty());
		level = new String(getLevel());
		myTextPaint = new Paint();
		myToastPaint = new Paint();
		myImagePaint = new Paint();
		myToastBGPaint = new Paint();
		myNextPiecePaint = new Paint();
		myScorebgPaint = new Paint();
		rectToast = new Rect(0, 0, 0, 0);
		rectNextPiece = new Rect(0, 0, 48, 48);
		rectNextPiecebg = new Rect(0, 0, 48, 48);
		rectScorebg = new Rect(0, 0, 0, 0);
		bla = new Picture();
		nextPiece = new PictureDrawable(bla);
		nextPiece.setAlpha(0);
		
		myTextPaint.setStyle(Paint.Style.FILL);
		myTextPaint.setAntiAlias(true);
		myTextPaint.setTextSize(15);
		myTextPaint.setTextAlign(Align.CENTER);
		myTextPaint.setAlpha(255);
		myTextPaint.setColor(Color.WHITE);
		myTextPaint.setFilterBitmap(true);
		myToastPaint.setStyle(Paint.Style.FILL);
		myToastPaint.setAntiAlias(true);
		myToastPaint.setTextSize(20);
		myToastPaint.setTextAlign(Align.CENTER);
		myToastPaint.setColor(Color.WHITE);
		myToastPaint.setAlpha(255);
		myToastBGPaint.setAlpha(255);
		myToastBGPaint.setAntiAlias(true);
		myToastBGPaint.setDither(true);
		myImagePaint.setDither(true);
		myImagePaint.setAntiAlias(true);
		myImagePaint.setTextAlign(Align.CENTER);
		myNextPiecePaint.setColor(Color.GRAY);
		myNextPiecePaint.setAlpha(127);
		myScorebgPaint.setColor(Color.LTGRAY);
		myScorebgPaint.setAlpha(127);
		
		w = GamePlayView.mWindowWidth;
		h = GamePlayView.mWindowHeight;
		
		background = new Rect(0, 0, w, h);
		
		v = (Vibrator) mContext.getSystemService(Context.VIBRATOR_SERVICE);
	}
	
	@Override
	public void draw(Canvas canvas) {
		//picture = Shr_Res.level;
		level = getLevel();
		
		//===================Portrait Settings======================
		//   The Game Board is in 2/3 of the width screen
		//   The Score Board is in 1/3 of the width screen 
		//==========================================================
		if(Shr_Res.screenOrientation == true){  //Portrait
			//Draw the first picture as background
			canvas.drawBitmap(bgImagePort[Shr_Res.level], null, background, myImagePaint);
			
			gameRectBackground.set(0, 0, (float)w/3*2, (float)h);
			//rectNextPiece.set(w/3*2+w/3/2-nextPiece.getIntrinsicWidth()/2, h/5*2, w/3*2+w/3/2+nextPiece.getIntrinsicWidth()/2, h/5*2+nextPiece.getIntrinsicHeight());
			rectNextPiece.set(w/3*2+w/3/2-pw*2, h/5*2, w/3*2+w/3/2+pw*2, h/5*2+ph*4); 
			rectNextPiecebg.set(rectNextPiece.left-3, rectNextPiece.top-3, rectNextPiece.right+3, rectNextPiece.bottom+3);
			rectScorebg.set(w/3*2+5, 0, w-5, rectNextPiece.bottom+7);
			
			canvas.drawText(dificulty, (float)w/3*2+w/3/2, 15, myTextPaint);
			
			canvas.drawText("Level", (float)w/3*2+w/3/2, 35, myTextPaint);
			canvas.drawText(level, w/3*2+w/3/2, 50, myTextPaint);
			
			canvas.drawText("Gravity", w/3*2+w/3/2, 70, myTextPaint);
			canvas.drawText(Physics.gravity+" m/s2", w/3*2+w/3/2, 85, myTextPaint);
			
			canvas.drawText("Score", w/3*2+w/3/2, 105, myTextPaint);
			checkScore();
			canvas.drawText(Integer.toString(Board.score), w/3*2+w/3/2, 120, myTextPaint);   //Prints the score
			
			canvas.drawText("Next Piece", (float)w/3*2+w/3/2, h/5*2-5, myTextPaint);
			canvas.drawRect(rectScorebg, myScorebgPaint);
			
			menu.setPosition(w/3*2+w/3/2-menuButton.getWidth()/2, h/5*4);
			pause.setPosition(w/3*2+w/3/2-pauseButton.getWidth()/2, h/5*4-pauseButton.getHeight()-15);
			
//			if (Shr_Res.blockWasForm){
//				canvas.drawText(toast, (float)w/2-(toast.length()/2), h/2, myTextPaint);
//				Shr_Res.blockWasForm = false;
//			}
			
		}
		//===================Landscape Settings======================
		//   The Game Board is in 3/4 of the width screen
		//   The Score Board is in 1/4 of the width screen
		//==========================================================		
		if(Shr_Res.screenOrientation == false){
			//Draw the first picture as background
			//canvas.drawBitmap(bgImageLand[picture], 0, 0, myTextPaint);
			canvas.drawBitmap(bgImageLand[Shr_Res.level], null, background, myImagePaint);
			
			gameRectBackground.set((float)0, (float)0, (float)w/4*3, (float)h);
			
			rectNextPiece.set(w/4*3+w/4/2-pw*2, h/4*2-15, w/4*3+w/4/2+pw*2, h/4*2+ph*4-15);
			rectNextPiecebg.set(rectNextPiece.left-3, rectNextPiece.top-3, rectNextPiece.right+3, rectNextPiece.bottom+3);
			rectScorebg.set(w/4*3+5, 0, w-5, rectNextPiece.bottom+5);
			
			canvas.drawText(dificulty,w/4*3+w/4/2, 13, myTextPaint);
			
			canvas.drawText("Level: "+level, w/4*3+w/4/2, 30, myTextPaint);
			//canvas.drawText(level, w/4*3+w/4/2, 50, myTextPaint);
			
			canvas.drawText("Gravity", w/4*3+w/4/2, 47, myTextPaint);
			canvas.drawText(Physics.gravity+" m/s2", w/4*3+w/4/2, 62, myTextPaint);
			
			canvas.drawText("Score", w/4*3+w/4/2,79 , myTextPaint);
			checkScore();
			canvas.drawText(Integer.toString(Board.score), w/4*3+w/4/2, 94, myTextPaint);   //Prints the score
			
			canvas.drawText("Next Piece", (float)w/4*3+w/4/2, h/4*2-23, myTextPaint);
			
			canvas.drawRect(rectScorebg, myScorebgPaint);
			
			pause.setPosition(w/4*3,h/4*3+5);
			menu.setPosition(w/4*3+pauseButton.getWidth(), h/4*3+5);
			
//			if (Shr_Res.blockWasForm){
//				canvas.drawText(toast, (float)w/2-(toast.length()/2), h/2, myTextPaint);
//				Shr_Res.blockWasForm = false;
//			}
		}
		//===============About the Game Background=======================
		//Draws game area background
		canvas.drawBitmap(gameImageBackground, null, gameRectBackground, myTextPaint);
		
		
		//================About the Change Image Button======================
		//Draws the "changebgpicture" button when it is not pressed
/*		if(!menu.isButtonPressed(Shr_Res.touchXPosition, Shr_Res.touchYPosition)){
			menu.drawFakeButton(canvas, menuButton, myTextPaint);
		}
		//Draws the "changebgpicture" button when it is pressed
		if(menu.isButtonPressed(Shr_Res.touchXPosition, Shr_Res.touchYPosition)){
			if (Shr_Res.screenTouched){
				menu.drawFakeButton(canvas, menuButtonPressed, myTextPaint);
			}
			else if(!Shr_Res.screenTouched){
				menu.drawFakeButton(canvas, menuButton, myTextPaint);
				Shr_Res.touchXPosition = 0;
				Shr_Res.touchYPosition = 0;
				picture++;                          //Change bg picture bitmap
				if(picture > 3){picture=0;}         //check if it is the last picture, if it is goes to the first picture
			}
		}
*/
		//=================== About the Menu Button ========================
		if(!menu.isButtonPressed(Shr_Res.touchXPosition, Shr_Res.touchYPosition)){
			menu.drawFakeButton(canvas, menuButton, myTextPaint);
		}
		if(menu.isButtonPressed(Shr_Res.touchXPosition, Shr_Res.touchYPosition)){
			if (Shr_Res.screenTouched){
				menu.drawFakeButton(canvas, menuButtonPressed, myTextPaint);
			}
			else if(!Shr_Res.screenTouched){
				menu.drawFakeButton(canvas, menuButton, myTextPaint);
				Shr_Res.touchXPosition = 0;
				Shr_Res.touchYPosition = 0;
			}
		}
		//===================About the Pause Button==========================		
		if (!Shr_Res.pauseGame){   //Game is not in pause
		//Draws the "pause" button when it is not pressed
			if(!pause.isButtonPressed(Shr_Res.touchXPosition, Shr_Res.touchYPosition)){
				pause.drawFakeButton(canvas, pauseButton, myTextPaint);
			}
			//Draws the "pause" button when it is pressed
			if(pause.isButtonPressed(Shr_Res.touchXPosition, Shr_Res.touchYPosition)){
				if(Shr_Res.screenTouched){
					pause.drawFakeButton(canvas, pauseButtonPressed, myTextPaint);
				}				
				else if(!Shr_Res.screenTouched){
					pause.drawFakeButton(canvas, pauseButton, myTextPaint);
					Shr_Res.touchXPosition = 0;
					Shr_Res.touchYPosition = 0;
				}
			}
		}else{   //Game is paused
			//Draws the "pause" button when it is not pressed
			if(!pause.isButtonPressed(Shr_Res.touchXPosition, Shr_Res.touchYPosition)){
				pause.drawFakeButton(canvas, playButton, myTextPaint);
			}
			//Draws the "pause" button when it is pressed
			if(pause.isButtonPressed(Shr_Res.touchXPosition, Shr_Res.touchYPosition)){
				if(Shr_Res.screenTouched){
					pause.drawFakeButton(canvas, playButtonPressed, myTextPaint);
				}				
				else if(!Shr_Res.screenTouched){
					pause.drawFakeButton(canvas, playButton, myTextPaint);
					Shr_Res.touchXPosition = 0;
					Shr_Res.touchYPosition = 0;
				}
			}
		}
		//===================About the Next Piece==========================
		canvas.drawRect(rectNextPiecebg, myNextPiecePaint);
		if (Shr_Res.showNextPiece){
			nextPiece.setAlpha(0);
			nextPiece.setBounds(rectNextPiece);
			nextPiece.draw(canvas);
		}
	}
	
	public String getLevel(){
		String planet = "null";
		switch (Shr_Res.level){
		case 0:
			planet = "Moon";
			break;
		case 1:
			planet = "Mars";
			break;
		case 2:
			planet = "Earth";
			break;
		case 3:
			planet = "Saturn";
			break;
		case 4:
			planet = "Jupiter";
			break;
		case 5:
			planet = "Sun";
			break;
		}
		return planet;
	}
	
	public String getDificulty(){
		String dificulty = "null";
		switch (Shr_Res.dificulty){
		case 0:
			dificulty = "Easy";
			break;
		case 1:
			dificulty = "Medium";
			break;
		case 2:
			dificulty = "Hard";
			break;
		case 3:
			dificulty = "Insane";
			break;
		}
		return dificulty;
	}
	
	public void setNextPiece(PictureDrawable piece){
		nextPiece = piece;
	}
	
	public void setWindowDimensions( int windowWidth, int windowHeight){
		w = windowWidth;
		h = windowHeight;
	}
	
	public void setPieceUnitSize(int width, int height){
		pw = width;
		ph = height;
		Log.d(Shr_Res.TAG, "BoardUI Unit Piece Width ="+pw);
		Log.d(Shr_Res.TAG, "BoardUI Unit Piece Height ="+ph);
	}
	
	private void checkScore(){
		if (Board.score > 10000 && Board.score < 20000 && Shr_Res.level == 0){
			Shr_Res.level++;
			framework.Physics.gravity = framework.Physics.getGravity();
			v.vibrate(1000);
		}
		if (Board.score > 20000 && Board.score < 30000 && Shr_Res.level == 1){
			Shr_Res.level++;
			framework.Physics.gravity = framework.Physics.getGravity();
			v.vibrate(1000);
		}
		if (Board.score > 30000 && Board.score < 40000 && Shr_Res.level == 2){
			Shr_Res.level++;
			framework.Physics.gravity = framework.Physics.getGravity();
			v.vibrate(1000);
		}
		if (Board.score > 40000 && Board.score < 50000 && Shr_Res.level == 3){
			Shr_Res.level++;
			framework.Physics.gravity = framework.Physics.getGravity();
			v.vibrate(1000);
		}
		if (Board.score > 50000 && Board.score < 60000 && Shr_Res.level == 4){
			Shr_Res.level++;
			framework.Physics.gravity = framework.Physics.getGravity();
			v.vibrate(1000);
		}
	}
	
	public void showToast(Canvas canvas){
		rectToast.set(w/2-((toast.length()/2)*((int)myToastPaint.getTextSize())),
				(int) (h/2-myToastPaint.getTextSize()),
				w/2+((toast.length()/2)*((int)myToastPaint.getTextSize())),
				h/2+((int)myToastPaint.getTextSize()/2));
		canvas.drawBitmap(toastBackground, null, rectToast, myToastBGPaint);
		canvas.drawText(toast, (float)w/2-(toast.length()/2), h/2, myToastPaint);
		myToastPaint.setAlpha(255-(255/toastDelay*toastState));
		myToastBGPaint.setAlpha(255-(255/toastDelay*toastState));
		if(toastState >= toastDelay){
			Shr_Res.blockWasForm = false;
			toastState = 0;
		}
		toastState++;
	}
	
}

